<?php
    require_ONCE("global.php");
    require_ONCE("HttpApi.php");
    
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        exit();
    }
    /*
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    */
    if ($_FILES["file"]["size"] < 200000000)
    {
        if (!isset($_SESSION['user']))
        {
            header("Location:login.php");
            exit();
        }
        
        $userid = $_POST["ownerid"];
        $parentid = $_POST["parentid"];
        $path = $_POST["path"];
        $name = $_FILES["file"]["name"];
        $url = 'http://'.$DS_HOST.'/'.$userid.'/'.$parentid.'/writefile?name='.$name;
     
        $content = file_get_contents($_FILES["file"]["tmp_name"]);

        $http =  new HttpApi();
        $http->setFileUploadSupport(true);
        $strres = $http->makeRequest($url, $content); //store data with url encode
        $result = json_decode($strres);
        $result = get_object_vars($result);
        //var_dump($result);
        if ($result["result"] === 0)
        {
            $parentname = getdirname($userid, $parentid);
            oplog($userid, $_SESSION['user']["id"], $_SESSION['user']["name"], "browser", $parentname, "web", $name, NULL, "upload file ".$name. " in directory ".$parentname);
            include "templates/success.html";
        }
        else
        {
            print_r($strres);
        }
    }
    else
    {
        echo "Invalid file, need to be smaller than 200M";
    }
?>
